package com.wzy.common.base.util;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.StringTokenizer;
import java.util.stream.Collectors;

/**
 * 2 * @Author: wuzhengyu
 * 3 * @Date: 2021/4/7 10:40
 * 4
 */
public class ResolveUtil {

    public static List<String> article2Sentence(String article) {

        // 将中文空格替换为英文空格
        article = article.replace((char) 12288, ' ');
        article = article.replace('\n', ' ');
        // 正则表达式分割\过滤\添加.(这里需要注意的是类似';'的符号是没办法分开的)
        return Arrays.stream(article.split("(\\.|\\!|\\?|。|！|？|\\.{6})"))
                .filter(t -> t != null && !t.equals(""))
                .filter(t -> t.length() >2)
                .map(String::trim)
                .map(t -> t + ".")
                .collect(Collectors.toList());

    }

    public static List<String> sentence2Word(String sentence) {
        sentence = sentence.replace((char) 12288, ' ');
        StringTokenizer st = new StringTokenizer(sentence," ,?.;!:\"\"\n#");
        List<String> wordList = new ArrayList<>();
        while (st.hasMoreElements()) {
            wordList.add(st.nextToken().toLowerCase());
        }
        return wordList.stream()
                .filter(t -> t!=null && !t.equals(""))
                .map(String::trim)
                .distinct() // 去重
                .collect(Collectors.toList());
    }


    public static void main(String[] args) {
        //for (String s : article2Sentence("May you have enough happiness to make you sweet,enough trials to make you strong,enough sorrow to keep you human,enough hope to make you happy? Always put yourself in others’shoes.If you feel that it hurts you,it probably hurts the other person, too.\n" +
        //        "\n" +
        //        "　　The happiest of people don’t necessarily have the best of everything;they just make the most of everything that comes along their way.Happiness lies for those who cry,those who hurt, those who have searched,and those who have tried,for only they can appreciate the importance of people")) {
        //    System.out.println("=============");
        //    System.out.println(s);
        //}
        for (String s : sentence2Word("May you have enough happiness to make you sweet,enough trials to make you strong,enough sorrow to keep you human,enough hope to make you happy? Always put yourself in others’shoes.If you feel that it hurts you,it probably hurts the other person, too.\n" +
                "\n" +
                "　　The happiest of people don’t necessarily have the best of everything;they just make the most of everything that comes along their way.Happiness lies for those who cry,those who hurt, those who have searched,and those who have tried,for only they can appreciate the importance of people")) {
            System.out.println(s);
        }


    }




}
